package com.copyctrl.campus.portal.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.copyctrl.campus.portal.dto.GetDTO;
import com.copyctrl.campus.portal.service.GetService;
import com.copyctrl.mapper.CampGetMapper;
import com.copyctrl.mapper.CampUserMapper;
import com.copyctrl.model.CampGet;
import com.copyctrl.model.CampGetExample;
import com.copyctrl.model.CampUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author:JH
 * @version:1.0
 */
@Service
public class GetServiceImpl implements GetService {
    @Autowired
    CampGetMapper mapper;
    @Autowired
    CampUserMapper userMapper;
    @Override
    public int add(CampGet get) {
        return mapper.insert(get);
    }

    @Override
    public List<GetDTO> findByGid(int gid) {
        CampGetExample campGetExample = new CampGetExample();
        campGetExample.createCriteria().andFidEqualTo(gid);
        List<GetDTO> collect = mapper.selectByExample(campGetExample).stream().map(get -> {
            GetDTO getDTO = BeanUtil.copyProperties(get, GetDTO.class);
            Integer uid = getDTO.getUid();
            CampUser user = userMapper.selectByPrimaryKey(uid);
            getDTO.setUser(user);
            return getDTO;
        }).collect(Collectors.toList());
        return collect;
    }
}
